package org.eclipse.epsilon.emc.rdbms.core.imodeltransactionsupport;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.epsilon.emc.rdbms.core.persistancebroker.PersistanceBroker;
import org.eclipse.epsilon.emc.rdbms.core.persistanceobject.PersistanceObject;
import org.eclipse.epsilon.emc.rdbms.core.persistanttransaction.PersistanceTransaction;
import org.eclipse.epsilon.eol.models.transactions.IModelTransactionSupport;
import org.eclipse.epsilon.emc.rdbms.core.imodeltransactionsupport.*;

public class imodelTransactionSupport implements IModelTransactionSupport{

	PersistanceTransaction transaction = new PersistanceTransaction();	
	
	/**
	 * This begins a database transaction
	*/		
	@Override
	public void startTransaction()
	{ 
		PersistanceTransaction transaction = new PersistanceTransaction();	
			try {
				transaction.addTransaction();
			} 
			catch (SQLException e) 
			{
			    try {
					transaction.rollbackTransaction();
				} 
			    catch (SQLException e1) 
			    {
				
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
    }

	/**
	 * This commits a database transaction
	*/		
	@Override
	public void commitTransaction() 
	{
		try {
			transaction.commitTransaction();
			
		    } 
		catch (SQLException e) 
		{
			
			rollbackTransaction();
		}
	}

	/**
	 * This rollsback the database transaction
	*/		
	@Override
	public void rollbackTransaction() 
	{
		try 
		{
			transaction.rollbackTransaction();
		} 
		catch (SQLException e) 
		{
			
			e.printStackTrace();
		}
	}
	
	/**
	 * This processes the transactions a database transaction
	* @return result. This returns the result set from the transaction.
	*/		
	public ResultSet processTransaction()
	{
		try
		{
			ResultSet result = transaction.processTransaction();
			return result;
		}
		catch (SQLException e)
		{
			e.getMessage();
			return null;
		}
	}
}
